package com.zh.note.huawei.onehundred.最少交换次数;

/**
 * @Author: zhanghuan
 * @date: 2023/9/1 18:41
 * @description: create it
 */

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // String s = scanner.nextLine();
        String s = "1 3 1 4 0";
        int k = 2;
        String[] numsStr = s.split(" ");
        int[] nums = new int[numsStr.length];
        for (int i = 0; i < numsStr.length; i++) {
            nums[i] = Integer.parseInt(numsStr[i]);
        }

        // int k = Integer.parseInt(scanner.nextLine());


        int[] newNums = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < k) {
                newNums[i] = 1;
            } else {
                newNums[i] = 0;
            }
        }

        int m = 0;
        for (int num : newNums) {
            m += num;
        }
        int res = solution(newNums, m);

        System.out.println(res);

        scanner.close();
    }

    //求一个长度等于数组中1的个数的滑窗，这个滑窗中0的个数最少,m是1的个数
    private static int solution(int[] nums, int m) {
        int i = 0;//滑动窗口起始位置
        int minZero = Integer.MAX_VALUE;
        int count = 0;
        //j为滑动窗口结束位置
        for (int j = 0; j < nums.length; j++) {
            if (nums[j] == 0) {
                count++;
            }
            if (j - i + 1 == m) { //如果滑窗等于k，更新最小0的值
                minZero = Math.min(minZero, count);
                if (nums[i] == 0) {
                    count--;
                }
                i++;
            }
        }
        return minZero == Integer.MAX_VALUE ? 0 : minZero;

    }

}

